package model

import (
	"crypto/md5"
	"encoding/hex"
	"errors"
	"github.com/google/uuid"
	"ldaccount/pkg/util"
	"strings"
	"time"
)

const (
	AccountsUnActivate int8 = 1
	AccountsOk         int8 = 2
	//ACCOUNTS_BLOCKED  int8  = 3

	AccountsUnActivateStr string = "unactivate"
	AccountsOkStr         string = "ok"
)

// Accounts 账户基本信息
type Accounts struct {
	ID          int64     `json:"id" sql:"id"`
	GID         string    `json:"g_id" sql:"g_id"`           // 全局 uuid
	Mobile      string    `json:"mobile" sql:"mobile"`       // 手机号
	Email       string    `json:"email" sql:"email"`         // 邮箱
	Status      int8      `json:"status" sql:"status"`       // 状态 1~未激活, 2~正常, 3~封停
	Password    string    `json:"password" sql:"password"`   // 密码
	UserName    string    `json:"user_name" sql:"user_name"` // 用户名
	Avatar      string    `json:"avatar" sql:"avatar"`
	CountryCode string    `json:"country_code" sql:"country_code"` // 国家码
	Origin      string    `json:"origin" sql:"origin"`             // 注册来源
	CreatedAt   time.Time `json:"created_at" sql:"created_at"`     // 创建时间
	UpdatedAt   time.Time `json:"updated_at" sql:"updated_at"`     // 更新时间
	DeletedAt   int64     `json:"deleted_at" sql:"deleted_at"`
}

// NewAccounts 新建账户
func NewAccounts(mobile, email, name, avatar, countryCode, origin string) *Accounts {
	uid, _ := uuid.NewUUID()
	now := util.GetNow()
	return &Accounts{
		GID:         strings.ReplaceAll(uid.String(), "-", ""),
		Mobile:      mobile,
		Email:       email,
		Status:      AccountsOk,
		UserName:    name,
		Avatar:      avatar,
		CountryCode: countryCode,
		Origin:      origin,
		CreatedAt:   now,
		UpdatedAt:   now,
	}
}

func md5Cal(pwd string) string {
	h := md5.New()
	h.Write([]byte(pwd))
	return strings.ToLower(hex.EncodeToString(h.Sum(nil)))
}

// AllowLogin 是否允许登陆
func (a *Accounts) AllowLogin() error {
	switch a.Status {
	case AccountsUnActivate:
		return errors.New("账号未激活")
	//case ACCOUNTS_BLOCKED:
	//	return errors.New("账号被封锁")
	default:
		return nil
	}
}

// PasswordVerify 密码校验
func (a *Accounts) PasswordVerify(pwd string, isMD5 bool) bool {
	if pwd == "" {
		return false
	}
	if !isMD5 {
		pwd = md5Cal(pwd)
	}
	pwd = md5Cal(pwd)
	return a.Password == pwd
}

// SetMobile 设置密码
func (a *Accounts) SetMobile(mobile string) {
	if mobile == "" {
		return
	}
	a.Mobile = mobile
	a.UpdatedAt = util.GetNow()
}

// SetPassword 设置密码
func (a *Accounts) SetPassword(pwd string, isMD5 bool) {
	if pwd == "" {
		return
	}
	if !isMD5 {
		pwd = md5Cal(pwd)
	}
	a.Password = md5Cal(pwd)
	a.UpdatedAt = util.GetNow()
}

func (a *Accounts) SetStatus(s int8) error {
	switch s {
	case AccountsUnActivate, AccountsOk:
		a.Status = s
	default:
		return errors.New("invalid status")
	}
	a.UpdatedAt = util.GetNow()
	return nil
}

func (a *Accounts) GetStatus() string {
	switch a.Status {
	case AccountsUnActivate:
		return AccountsUnActivateStr
	case AccountsOk:
		return AccountsOkStr
	default:
		return ""
	}
}

// IsActivate  已经激活
func (a *Accounts) IsActivate() bool {
	return a.Status == AccountsOk
}

// Activate 未激活
func (a *Accounts) Activate() {
	_ = a.SetStatus(AccountsOk)
}

func (a *Accounts) Delete() {
	a.DeletedAt = util.GetCurrentTimestamp()
}

// UpdateInfo 更新信息
func (a *Accounts) UpdateInfo(mobile, email, userName, avatar, countryCode string) {
	if email != "" {
		a.Email = email
	}
	if mobile != "" {
		a.Mobile = mobile
	}
	if userName != "" {
		a.UserName = userName
	}
	if countryCode != "" {
		a.CountryCode = countryCode
	}
	if avatar != "" {
		a.Avatar = avatar
	}
	a.UpdatedAt = util.GetNow()
}
